define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config;

    require('./paginator');
    require('./talbe-search')

    var $pagination = $("ul.pagination");
    $pagination.length > 0 && $pagination.Paginator({
        totalPages: $pagination.data('pagesTotal'),
        currentPage: $pagination.data('pageCurrent'),
        page: function(p) {
            $('#pageNumber').val(p).trigger('pageNumber:change')
            return true;
        }
    }); 
    var $tabletable_responsive = $("div.table-responsive"),ttable=false,$firsttable="",$lasttable="";
    var $checkbox_list = $('table tbody tr td:first-child [type="checkbox"]', $tabletable_responsive);
    var $batch = $('[data-toggle^="batch"]');
    var $trigger_batch = $('[data-trigger="batch"]');
    if($("div.table-responsive table.table-scroll").length>1){
        ttable=true;
        $firsttable=$("div.table-responsive.tab-content:first");
        $lasttable=$("div.table-responsive.tab-content:last");
        //$("table",$firsttable).prepend($("col",$lasttable).clone());
        var offset=$lasttable.data("offset") || 0;
        $lasttable.css("max-height",$("body").height()-$firsttable.height()-118-offset);
        $lasttable.scroll(function() {
            $firsttable.scrollLeft($lasttable.scrollLeft());
        })
    }
    if ($checkbox_list.length > 0) {
        var get_list = function() {
            var ckbox_list = $('table tbody tr td:first-child [type="checkbox"]:checked', $tabletable_responsive);
            var data = ckbox_list.map(function() {
                return $(this).val();
            }).get().join(',');
            return {
                ckbox_list: ckbox_list,
                values: data
            }
        }
        $batch.on("click", function() {
            var $this = $(this),
                href = $(this).data('href'),
                org = $this.html();
            config.msg.msgbox.confirm($this.data('msg') || config.lang.confirmAllremove, function(result) {
                if (result) {
                    $this.attr('disabled', 'disabled');
                    var ckbox_list = $('table tbody tr td:first-child [type="checkbox"]:checked', $tabletable_responsive);
                    var data = ckbox_list.map(function() {
                        return $(this).val();
                    }).get();
                    $this.html('<i class="fa fa-spinner fa-spin"></i>');
                    $.post(href, {
                        "ids": data.join(',')
                    }).done(function(d) {
                        if (config.issucceed(d)) {
                            config.msg.suc(d.message || config.lang.removeSuccess, d.url)
                            var dtd = $.Deferred();
                            var wait = function(dtd) {　　
                                var i = 0;
                                ckbox_list.parents('tr').fadeOut(function() {
                                    $(this).remove();
                                    i++;
                                    ckbox_list.length == i && dtd.resolve();
                                });
                                return dtd;　　
                            };
                            $.when(wait(dtd)).done(function() {
                                $batch.addAttr("disabled", $('table tbody tr td:first-child [type="checkbox"]:checked', $tabletable_responsive).length == 0);
                                $('table tbody tr', $tabletable_responsive).length == 0 && window.location.reload()
                            })


                        } else {
                            config.msg.error(d.message || config.lang.removeError, d.url)
                        }
                        $this.html(org)
                    }).fail(function() {
                        $this.html(org)
                        config.msg.info(config.lang.exception)
                    })
                }
            });

        });
        $trigger_batch.on("click", function() {
            var data = get_list();
            $(this).data("set", {ids:data.values,form:$(this).closest('form').serializeArray()});
        });
        $(document).on('change', 'div.table-responsive table thead th:first [type="checkbox"]', function(e) {
            e && e.preventDefault();
            var $table = ttable?$("div.table-responsive.tab-content:last table.table-scroll"):$(e.target).closest('table'),
                $checked = $(e.target).is(':checked');
            $('tbody tr td:first-child  [type="checkbox"]', $table).prop('checked', $checked);
            $batch.add($trigger_batch).addAttr("disabled", !$checked);
        })
        $(document).on('change', 'div.table-responsive table tbody td:first-child [type="checkbox"]', function(e) {
            e && e.preventDefault();
            var $table = $(e.target).closest('table'),
                $table1 = ttable?$("div.table-responsive.tab-content:first table.table-scroll"):$(e.target).closest('table'),
                $checked = $(e.target).is(':checked'),
                $checkbox_list_checked = $('tbody tr td:first-child  [type="checkbox"]:checked', $table);
            $('thead th:first [type="checkbox"]', $table1).prop('checked', $checked && $checkbox_list_checked.length == $checkbox_list.length);
            $batch.add($trigger_batch).addAttr("disabled", $checkbox_list_checked.length == 0);
        })
    }


    $(document).on('click', 'div.table-responsive table tbody [data-toggle="ajaxRemove"]',
        function(e) {
            e.preventDefault();
            var $this = $(this),
                $remote = $this.data('remote') || $this.attr('href'),
                $confirm = $this.data('confirm');
            $confirm = (typeof $confirm == "undefined" ? true : $confirm);
            $this.data("loadingText", '<i class="fa fa-spinner fa-spin"></i>')
            var done = function() {
                var org = $this.html();
                $this.button('loading');
                $.post($remote).done(function(d) {
                    if (config.issucceed(d)) {
                        config.msg.suc(d.message || config.lang.removeSuccess, d.url)
                        var $tr = $this.parents('tr');
                        $tr.css({
                            'background-color': '#dff0d8'
                        }).find('td').css({
                            'background-color': '#dff0d8'
                        });
                        $tr.fadeOut(function() {
                            $tr.remove();
                            $('table tbody tr').length == 0 && window.location.reload()
                        })
                    } else {
                        $this.button('reset');
                        config.msg.error(d.message || config.lang.removeError, d.url)
                    }
                }).fail(function() {
                    $this.button('reset');
                    config.msg.info(config.lang.exception)
                });

            }
            $confirm && config.msg.msgbox.confirm($this.data('msg') || config.lang.confirmRemove, function(result) {
                if (result) {
                    done();
                }
            });
            !$confirm && done();
        });




});
